package com.example.demo.controller;

import com.example.demo.controller.dto.DoctorFlowDTO;
import com.example.demo.controller.vo.DoctorFlowVO;
import com.example.demo.controller.vo.DoctorInfoVO;
import com.example.demo.dao.DoctorDao;
import com.example.demo.dao.FlowDao;
import com.example.demo.entity.Doctor;
import com.example.demo.entity.DoctorFlow;
import com.example.demo.utils.QRCodeUtil;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WinstonDeng
 * @Description:
 * @Date: Created in 9:40 2020/4/9
 * @Modified by:
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorDao doctorDao;

    @Autowired
    private FlowDao flowDao;


    @GetMapping("/{doctorId}")
    @ResponseBody
    public ResponseEntity<Result> getDoctorById(@PathVariable("doctorId") long doctorId) throws Exception{
        Doctor doctor = doctorDao.getById(doctorId);
        return ResponseEntity.ok(ResultUtil.success(new DoctorInfoVO(doctor)));
    }

    @GetMapping("/getByName")
    @ResponseBody
    public ResponseEntity<Result> getDoctorListByName(@RequestParam("name") String doctorName) throws Exception{
        if (doctorName.isEmpty()){
            return ResponseEntity.ok(ResultUtil.error(417,"姓名不能为空"));
        }
        List<DoctorInfoVO> doctorInfoVOList = new ArrayList<DoctorInfoVO>();
        List<Doctor> doctorList = doctorDao.getListByName(doctorName);
        for(Doctor doctor:doctorList){
            doctorInfoVOList.add(new DoctorInfoVO(doctor));
        }
        return ResponseEntity.ok(ResultUtil.success(doctorInfoVOList));
    }

    @GetMapping("/getByDepartmentName")
    @ResponseBody
    public ResponseEntity<Result> getDoctorListByDepartmentName(@RequestParam("departmentName") String departmentName) throws Exception{
        if(departmentName.isEmpty()){
            return ResponseEntity.ok(ResultUtil.error(417,"科室名不能为空"));
        }
        List<DoctorInfoVO> doctorInfoVOList = new ArrayList<DoctorInfoVO>();
        List<Doctor> doctorList = doctorDao.getListByDepartmentName(departmentName);
        for(Doctor doctor:doctorList){
            doctorInfoVOList.add(new DoctorInfoVO(doctor));
        }
        return ResponseEntity.ok(ResultUtil.success(doctorInfoVOList));
    }

    @PostMapping("/login")
    @ResponseBody
    public ResponseEntity<Result> doctorLogin(@RequestParam("username") String username,
                                              @RequestParam("password") String password,
                                              @RequestParam(name="openId", defaultValue = "") String openId) throws Exception{
        if(username.isEmpty()){
            return ResponseEntity.ok(ResultUtil.error(417, "用户名不能为空"));
        }
        if(password.isEmpty()){
            return ResponseEntity.ok(ResultUtil.error(417, "密码不能为空"));
        }
        Doctor doctor = doctorDao.getByUsername(username);
        if (doctor == null){
            return ResponseEntity.ok(ResultUtil.error(417, "用户名错误"));
        }
        if (doctor.getPassword().equals(password)){
            if(!openId.isEmpty()){
                if(doctor.getOpenId()==null || doctor.getOpenId().isEmpty()){
                    doctor.setOpenId(openId);
                    doctor = doctorDao.updateDoctor(doctor);
                }
            }
            return ResponseEntity.ok(ResultUtil.success(new DoctorInfoVO(doctor)));
        }else {
            return ResponseEntity.ok(ResultUtil.error(417, "用户名或密码错误"));
        }
    }

    @PostMapping("/insertDoctorFlow")
    @ResponseBody
    public ResponseEntity<Result> insertDoctorFlow(@RequestBody DoctorFlowDTO doctorFlowDTO) throws Exception{
        Long mainDoctorId = flowDao.getById(doctorFlowDTO.getFlowId()).getDoctorId();
        if (!mainDoctorId.equals(doctorFlowDTO.getDoctorId())){
            return ResponseEntity.ok(ResultUtil.error(417, "doctorId 不是 flow 的主医生"));
        }
        Doctor doctor = doctorDao.getById(doctorFlowDTO.getOtherId());
        DoctorFlow doctorFlow = flowDao.insertDoctorFlow(doctorFlowDTO.getFlowId(), doctorFlowDTO.getOtherId(), doctorFlowDTO.getDuty());
        DoctorFlowVO doctorFlowVO = new DoctorFlowVO();
        doctorFlowVO.setDuty(doctorFlow.getDuty());
        doctorFlowVO.setDescription(doctor.getDescription());
        doctorFlowVO.setHonor(doctor.getHonor());
        doctorFlowVO.setName(doctor.getRealName());
        return ResponseEntity.ok(ResultUtil.success(doctorFlowVO));
    }

    @PostMapping("/QRCode/{doctorId}")
    @ResponseBody
    public ResponseEntity<Result> getQRCode(HttpServletRequest request, @PathVariable("doctorId") String doctorId) throws Exception{
        String text = doctorId;
        String outQRCodeImage = "qrcode_"+doctorId+".jpg";
        String imgpath = null;
//        String destPath = request.getServletContext().getRealPath("resources/static/qrcode/"+outQRCodeImage);
        // 获取根目录
        final StringBuilder rootPath = new StringBuilder(System.getProperties().getProperty("user.home"));
        // 添加项目路径
        rootPath.append("/xmu_hospital/");
        // 添加二维码存放路径
        rootPath.append("qrcode/");
        String destPath = rootPath+outQRCodeImage;
        try {
            QRCodeUtil.encode(text, imgpath, destPath, true);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(500, "生成二维码错误：" + e.getMessage()));
        }
        String str = null;
        try {
            str = QRCodeUtil.decode(destPath);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(500, "解析二维码错误：" + e.getMessage()));
        }
        return ResponseEntity.ok(ResultUtil.success("/qrcode/"+outQRCodeImage));
    }
}
